﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;

namespace VS2048
{
    public class PaintedCell : ICell
    {
        private int _wid = 0;
        private int _hei = 0;
        private int _left = 0;
        private int _top = 0;
       
        private int _pWid = 0;

        private int _factor = 0;///
        private Random _rdm = null;

        private bool _currMerged = false;
        private string _disNum = "0";
        private Image _backImg = null;
        private Font _txtfont = new Font("Candara'Century Gothic", 16, FontStyle.Bold);//'Candara'Century Gothic'Courier'
        private Color _backColor = Color.Snow;
        private Color _textColor = Color.WhiteSmoke;
        private CellCoordinate _cellLoc;
        private CellShape _shape = CellShape.Ellipse;

        public CellShape Shape
        {
            get { return _shape; }
            set { _shape = value; }
        }

        public Rectangle DisplayRect
        {
            get
            {
                return new Rectangle(Left, Top, Width, Height);
            }
        }

        public int Width
        {
            get { return _wid; }
            set { _wid = value; }
        }

        public int Height
        {
            get { return _hei; }
            set { _hei = value; }
        }

        public int ParentWidth
        {
            get { return _pWid; }
            set { _pWid = value; }
        }

        public int Left
        {
            get { return _left; }
            set { _left = value; }
        }

        public int Top
        {
            get { return _top; }
            set { _top = value; }
        }

        public Font TextFont
        {
            get { return _txtfont; }
            set { _txtfont = value; }
        }

        public bool CurrentMerged
        {
            get
            {
                return _currMerged;
            }
            set
            {
                _currMerged = value;
            }
        }

        public Color TextColor
        {
            get { return _textColor; }
            set { _textColor = value; }
        }

        public CellCoordinate Coordinate
        {
            get { return _cellLoc; }
            set { _cellLoc = value; }
        }

        public string DisplayNum
        {
            get { return _disNum; }
            set { _disNum = value; }
        }

        public Image BackImage
        {
            get { return _backImg; }
            set { _backImg = value; }
        }

        public Color BackColor
        {
            get { return _backColor; }
            set { _backColor = value; }
        }

        public void Refresh()
        {

        }

        public void Render(Graphics g)
        {
            if (_rdm == null)
                _rdm = new Random(1);
            _factor = _rdm.Next(100);

            g.TranslateTransform(Left, Top);
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.TextRenderingHint = TextRenderingHint.AntiAlias;

            using (Brush rcbrsh = new SolidBrush(_backColor))
            using (Brush txtbrsh = new SolidBrush(_textColor))
            {
                SizeF sz = g.MeasureString(DisplayNum, _txtfont);
                float x = (this.Width - sz.Width) / 2;
                float y = (this.Height - sz.Height) / 2;

                if (_shape == CellShape.Rectangle)
                {
                    g.FillRectangle(rcbrsh, 0, 0, Width, Height);
                    if (_factor % 2 == 0) g.DrawRectangle(Pens.Orange, 0, 0, Width, Height);
                }
                else if (_shape == CellShape.Ellipse)
                {
                    g.FillEllipse(rcbrsh, 0, 0, Width, Height);
                    if (_factor % 2 == 0) 
                        g.DrawEllipse(Pens.Orange, 0, 0, Width, Height);
                }
                g.DrawString(_disNum, _txtfont, txtbrsh, new PointF(x, y));
            }
            g.ResetTransform();
        }
    }
}
